/**
 * 判断是否为空
 */
export function validateNull (val) {
  if (typeof val === 'boolean') {
    return false
  }
  if (typeof val === 'number') {
    return false
  }
  if (val instanceof Array) {
    if (val.length === 0) return true
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === '{}') return true
  } else {
    if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true
    return false
  }
  return false
}

export function validateArray (val) {
  if (Array.isArray) {
    return Array.isArray(val)
  } else {
    return Object.prototype.toString.call(val) === '[object Array]'
  }
}

/**
 *
 * @param hex 例如:"#23ff45"
 * @param opacity 透明度
 * @returns {string}
 */
export function hexToRgba(hex, opacity) {
  return "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
}

/**
 * 获取一个变淡或者变深的颜色
 * @param hex 颜色
 * @param level 等级 -1到1
 * @returns {string}
 */
export function getDarkOrLightColor(hex, level) {
  let red = parseInt("0x" + hex.slice(1, 3))
  let green = parseInt("0x" + hex.slice(3, 5))
  let blue = parseInt("0x" + hex.slice(5, 7))
  if (-1 < level && level < 0) { //变浅
    level = -level;
    return `rgb(${Math.floor((255 - red) * level + red)},${Math.floor((255 - green) * level + green)},${Math.floor((255 - blue) * level + blue)})`
  } else if (0 < level && level < 1) { //变深
    return `rgb(${Math.floor(red * (1 - level))},${Math.floor(green * (1 - level))},${Math.floor(blue * (1 - level))})`
  } else {
    return hex
  }
}
